package com.class4.servlets;

import com.class4.fruit.dao.FruitDao;
import com.class4.fruit.dao.impl.FruitDaoImpl;
import com.class4.fruit.pojo.Fruit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddServlet extends HttpServlet {
    //    doPost
    @Override
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
            throws ServletException, IOException {
        //1、设置字符编码集编码
        request.setCharacterEncoding("UTF-8");
        //2、获取水果名字
        String fname = request.getParameter("fname");
        //2、获取水果价格
        String priceStr = request.getParameter("price");
        //3、把价格从String转为Integer
        Integer price = Integer.parseInt(priceStr);
        String fcountStr = request.getParameter("fcount");
        Integer fcount = Integer.parseInt(fcountStr);
        String remark = request.getParameter("remark");
        //4、创建水果业务类
        FruitDao fruitDao = new FruitDaoImpl();
        //5、调用添加水果方法，添加水果数据
        boolean flag = fruitDao.addFruit
                (new Fruit(0, fname, price, fcount, remark));
        //6、输出添加水果执行结果
        System.out.println(flag?"添加成功":"添加失败");

    }
}
